package com.rzico.sbl.ui

import android.widget.TextView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.MessageData
import com.rzico.sbl.other.privacyStatus
import com.rzico.sbl.other.userRole
import com.rzico.sbl.ui.manager.MasterBarrelActivity
import com.rzico.sbl.ui.manager.TaxActivity
import com.rzico.sbl.ui.mine.WalletBillActivity
import com.rzico.sbl.ui.order.DeliverActivity
import com.rzico.sbl.ui.order.OrderActivity
import com.rzico.sbl.ui.order.OrderDetailActivity
import com.rzico.sbl.ui.report.other.goToOrderDetail
import com.rzico.sbl.viewmodel.MessageViewMode
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.helper.phoneReplaceWithStar
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter
import kotlin.text.orEmpty

class MessageSecondActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }
    private var mMessageType = ""

    override fun getViewModel() = get<MessageViewMode>()

    override fun initData() {
        initTitle(title = intent.getExtra("title"))

        mMessageType = intent.getExtra("type")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关消息记录！"
                }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(top = 18f, bottom = 18f))

                    mAdapter = SlimAdapter.creator()
                        .register<MessageData>(R.layout.item_msg_second) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_msg_name, intent.getExtra("title"))
                                text(R.id.item_msg_time, bean.createDate.getTimeString())
                                text(R.id.item_msg_content, bean.content)
                                text(R.id.item_msg_money, bean.ext?.amount.formatDecimal())

                                visibility(
                                    R.id.item_msg_hint,
                                    visibleValue { mMessageType == "0" && bean.ext != null }
                                )

                                visibility(
                                    R.id.item_msg_bottom,
                                    visibleValue {
                                        mMessageType != "3"
                                                && mMessageType in "0,1,13"
                                                && bean.ext != null
                                    }
                                )

                                val tvHint1 = getView<TextView>(R.id.item_msg_hint1)
                                val tvHint2 = getView<TextView>(R.id.item_msg_hint2)
                                val tvHint3 = getView<TextView>(R.id.item_msg_hint3)
                                val tvContent1 = getView<TextView>(R.id.item_msg_content1)
                                val tvContent2 = getView<TextView>(R.id.item_msg_content2)
                                val tvContent3 = getView<TextView>(R.id.item_msg_content3)

                                when (mMessageType) {
                                    "0" -> {
                                        tvHint1.text = "订单号："
                                        tvHint2.text = "订单状态："
                                        tvHint3.text = "下单信息："
                                        tvContent1.text = bean.ext?.sn.orEmpty()
                                        tvContent2.text = bean.ext?.statusDescr.orEmpty()
                                        tvContent3.text =
                                            "${bean.ext?.consignee.orEmpty()} - ${
                                                when {
                                                    userRole in "1,2"
                                                            || privacyStatus == "off" -> bean.ext?.phone.orEmpty()
                                                    else -> bean.ext?.phone.phoneReplaceWithStar()
                                                }
                                            }"
                                    }
                                    "1" -> {
                                        tvHint1.text = "付款方式："
                                        tvHint2.text = "账号余额："
                                        tvHint3.text = "商品说明："
                                        tvContent1.text = bean.ext?.method.orEmpty()
                                        tvContent2.text = "${bean.ext?.balance.formatDecimal()} 元"
                                        tvContent3.text = bean.ext?.memo.orEmpty()
                                    }
                                    "13" -> {
                                        tvHint1.text = "订单号："
                                        tvHint2.text = "订单状态："
                                        tvHint3.text = "下单信息："
                                        tvContent1.text =
                                            bean.ext?.orderSn
                                                .orEmpty()
                                                .ifEmpty {
                                                    bean.ext?.sn.orEmpty()
                                                }
                                        tvContent2.text = bean.ext?.statusDescr.orEmpty()
                                        tvContent3.text =
                                            "${bean.ext?.consignee.orEmpty()} - ${
                                                when {
                                                    userRole in "1,2"
                                                            || privacyStatus == "off" -> bean.ext?.phone.orEmpty()
                                                    else -> bean.ext?.phone.phoneReplaceWithStar()
                                                }
                                            }"
                                    }
                                }

                                clicked {

                                    when (mMessageType) {
                                        "0" -> {
                                            when {
                                                bean.ext != null
                                                        && bean.ext?.printStatus.isNullOrEmpty() -> {
                                                    startActivityEx<OrderDetailActivity>(
                                                        "orderSn" to bean.ext?.sn.orEmpty(),
                                                        "shippingSn" to bean.ext?.shippingSn.orEmpty()
                                                    )
                                                }
                                                bean.ext != null
                                                        && !bean.ext?.printStatus.isNullOrEmpty() -> {
                                                    goToOrderDetail(
                                                        "orderSn" to bean.ext?.sn.orEmpty()
                                                    )
                                                }
                                                "未派送新订单" in bean.content -> {
                                                    startActivityEx<OrderActivity>("index" to 2)
                                                }
                                                userRole in "1,2" -> startActivityEx<OrderActivity>()
                                            }
                                        }
                                        "1" -> startActivityEx<WalletBillActivity>()
                                        "13" -> {
                                            val orderSn = bean.ext?.orderSn
                                                .orEmpty()
                                                .ifEmpty {
                                                    bean.ext?.sn.orEmpty()
                                                }

                                            when {
                                                bean.ext != null
                                                        && bean.ext?.printStatus.isNullOrEmpty() -> {
                                                    startActivityEx<OrderDetailActivity>(
                                                        "orderSn" to orderSn,
                                                        "shippingSn" to bean.ext?.shippingSn.orEmpty()
                                                    )
                                                }
                                                bean.ext != null
                                                        && !bean.ext?.printStatus.isNullOrEmpty() -> {
                                                    goToOrderDetail("orderSn" to orderSn)
                                                }
                                                "订单无人派送" in bean.content -> {
                                                    startActivityEx<DeliverActivity>("index" to 1)
                                                }
                                            }
                                        }
                                        "14" -> startActivityEx<MasterBarrelActivity>()
                                        "15" ->
                                            if (bean.ext?.type!=null){
                                                if (bean.ext?.type=="common"){
                                                    startActivityEx<TaxActivity>(
                                                        "title" to "普通发票",
                                                        "status" to "common"
                                                    )
                                                }else{
                                                    startActivityEx<TaxActivity>(
                                                        "title" to "专用发票",
                                                        "status" to "special"
                                                    )
                                                }
                                            }


                                    }
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    /**
     * 消息二级列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .messageList(
                page = index,
                status = intent.getExtra("num"),
                type = mMessageType,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}